define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'bootstrap-daterangepicker'], function ($, undefined, Backend, Table, Form, Template, daterangepicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/exam/index' + location.search,
                    add_url: 'exam/exam/add',
                    edit_url: 'exam/exam/edit',
                    del_url: 'exam/exam/del',
                    multi_url: 'exam/exam/multi',
                    table: 'exam',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {
                            field: 'start_time',
                            title: __('Start_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'end_time',
                            title: __('End_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: false,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            visible: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('查看'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                extend:'data-area=\'["80%","80%"]\'',
                                url: 'exam/student/detail'
                            },{
                                name: 'report',
                                text: __('报告分析'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                extend:'data-area=\'["80%","80%"]\'',
                                url: 'exam/report/detail'
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('.school').change(function () {
                $.ajax({
                    url: 'common/subjects',
                    type: 'POST',
                    dataType: 'json',
                    data: {school_id: $(this).val()},
                    success: function (data) {
                        $('#subject').html(Template('tpl_subject', data));
                    },
                });
            });

            $('#subject').delegate('.subject', 'change', function () {
                if (this.checked) {
                    $('#subject_body').append(Template('tpl_subject_body', {
                        id: $(this).data('id'),
                        name: $(this).data('name')
                    }));
                } else {
                    $('.subject_' + $(this).data('id')).remove();
                }
            });

            $(document).on('click', '.download', function () {
                var subjects = '';
                $.each($('input:checkbox:checked'),function(){
                    subjects += $(this).val() + ',';
                });
                window.location.href = 'download?school_id=' + $('.school').val() + '&subjects=' + subjects;
            });

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $('.daterange').daterangepicker({
                    timePicker: false,
                    autoUpdateInput: false,
                    timePickerSeconds: true,
                    timePicker24Hour: true,
                    autoApply: true,
                    locale: {
                        format: 'YYYY-MM-DD HH:mm:ss',
                        customRangeLabel: __("Custom Range"),
                        applyLabel: __("Apply"),
                        cancelLabel: __("Clear"),
                    },
                }, function (start, end) {
                    $(this.element).val(start.format(this.locale.format) + " - " + end.format(this.locale.format));
                    $(this.element).trigger('blur');
                });


                Form.api.bindevent($("form[role=form]"));

            },
        }
    };
    return Controller;
});